package tech.xiaozai.rbac.security.authorization;

import cn.hutool.json.JSONUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import tech.xiaozai.common.restful.Error;
import tech.xiaozai.common.restful.RestResp;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-09-24 10:36
 *
 *  针对没登录的请求到来
 */
public class MyAuthenticationEndPoint implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setStatus(401);
        response.setContentType("application/json;charset=utf-8");
        RestResp error = RestResp.error(Error.SIGN_CHECK_ERROR);
        String jsonStr = JSONUtil.toJsonStr(error);
        response.getWriter().write(jsonStr);
        response.getWriter().flush();
    }
}
